﻿#pragma strict

var mousePosition : Vector3;
public var moveSpeed : float = 0.1f;
var destino : boolean = true;
var qtdProduzir : int;
var qtdProduzida : int;

//Victor.
private var leftClickedObject : GameObject;
private var rightClickedObject : GameObject;
private var frontmostRaycastHit : RaycastHit2D;
private var clickInMe: boolean;
private var disponivel : boolean;
//public Sprite spriteImage;
// It's necessary to access the SpriteRenderer of a game object to be able to access its sorting layer ID
// and sorting order ("Order in Layer" in the inspector)
private var spriteRenderer : SpriteRenderer;

function Start () {
    clickInMe = false;
    disponivel=false;
}



function Update () {
    
    
    if(Input.GetMouseButton(0))
    {
        // frontmostRaycastHit stores information about the RaycastHit2D that is returned by GetFrontmostRaycastHit()
        frontmostRaycastHit = GetFrontmostRaycastHit();
 
        // If frontmostRaycastHit is true, i,e the user hasn't clicked on nothing, i.e GetFrontmostRaycastHit() didn't return nothing...
        if (frontmostRaycastHit)
        {
            // Assigns the game object that the collider that has been clicked on to leftClickedObject.
            leftClickedObject = frontmostRaycastHit.collider.gameObject;

            Clique.object = leftClickedObject;
            
           
                       
            // Sends the frontmostRaycast to a function called OnLeftClick in a script attached to whatever the leftClickedObject is.
            leftClickedObject.SendMessage("OnLeftClick", leftClickedObject, SendMessageOptions.DontRequireReceiver);
            
        }
    }
    else if(Input.GetMouseButton(1))
    {
        // frontmostRaycastHit stores information about the RaycastHit2D that is returned by GetFrontmostRaycastHit()
        frontmostRaycastHit = GetFrontmostRaycastHit();

        
 
        // If frontmostRaycastHit is true, i,e the user hasn't clicked on nothing, i.e GetFrontmostRaycastHit() didn't return nothing...
        if (frontmostRaycastHit)
        {
            // Assigns the game object that the collider that has been clicked on to leftClickedObject.
            rightClickedObject = frontmostRaycastHit.collider.gameObject;            
                       
                       
            // Sends the frontmostRaycast to a function called OnLeftClick in a script attached to whatever the leftClickedObject is.
            rightClickedObject.SendMessage("OnRightClick", leftClickedObject, SendMessageOptions.DontRequireReceiver);
            
        }   
        else
        {
            OnRightClick();
        }

    }    

    if(destino==false)
    {
        transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
        if(transform.position == mousePosition)
        {
            destino = true;
        }
    }
    
}
	
function OnCollisionEnter2D(coll: Collision2D) {
	if (coll.gameObject.tag == "Deposito" && GUIgeral.deposito > 0)
	{
		//GetComponent (SpriteRenderer).sprite = spriteImage;
		Debug.Log("Deposito - Personagem");
		qtdProduzir = GUIgeral.deposito;
		GUIgeral.deposito = 0;
	}
	
	if(coll.gameObject.tag == "Maquina" && qtdProduzir > 0 && !disponivel)
	{
	    disponivel=true;
	    for(var i : int =1; i<=20; i++)
	    {
            Debug.Log(i.ToString() + "/20");
            yield WaitForSeconds(1);
	    }
		Debug.Log("Personagem - Maquina");
		qtdProduzida = qtdProduzir;
		qtdProduzir = 0;
	}
	
	if(coll.gameObject.tag == "Vendas" && qtdProduzida > 0)
	{
		Debug.Log("Maquina - Vendas (Vendeu)");
		GUIgeral.dinheiro+=qtdProduzida*20;
		qtdProduzida = 0;
	}

}


function OnLeftClick()
{
   
}

function OnRightClick()
{
    if(Clique.object == GameObject.FindWithTag("Mesh"))
    {
        mousePosition = Input.mousePosition;
        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);     
        destino=false;
    }  
}


function GetFrontmostRaycastHit(): RaycastHit2D
    {
        // Store the point where the user has clicked as a Vector3.
        var clickPosition : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        // Retrieve all raycast hits from the click position (to the same click position - more a dot than a ray) and store them in an array called "hits".
        var hits : RaycastHit2D[] = Physics2D.LinecastAll (clickPosition, clickPosition);
 
        // If the raycast hits something...
        if (hits.length != 0)
        {
            // A variable that will store the frontmost sorting layer that contains an object that has been clicked on as an int.
            var topSortingLayer : int = 0;
            // A variable that will store the index of the top sorting layer as an int.
            var indexOfTopSortingLayer : int;
            // An array that stores the IDs of all the sorting layers that contain a sprite in the path of the linecast.
            var sortingLayerIDArray : int[] = new int[hits.length];
            // An array that stores the sorting orders of each sprite that has been hit by the linecast
            var sortingOrderArray : int[] = new int[hits.length];
            // An array that stores the sorting order number of the frontmost sprite that has been clicked.
            var topSortingOrder : int = 0;
            // A variable that will store the index in the sortingOrderArray where topSortingOrder is. This index used with the hits array will give us our frontmost clicked sprite.
            var indexOfTopSortingOrder : int;
 
            // Loop through the array of raycast hits...
            for (var i = 0; i < hits.length; i++)
            {
                // Get the SpriteRenderer from each game object under the click.
                spriteRenderer = hits[i].collider.gameObject.GetComponent(SpriteRenderer);
               
                // Access the sortingLayerID through the SpriteRenderer and store it in the sortingLayerIDArray.
                sortingLayerIDArray[i] = spriteRenderer.sortingLayerID;
                       
                // Access the sortingOrder through the SpriteRenderer and store it in the sortingOrderArray.
                sortingOrderArray[i] = spriteRenderer.sortingOrder;
            }
               
            // Loop through the array of sprite sorting layer IDs...
            for (var j = 0; j < sortingLayerIDArray.length; j++)
            {
                // If the sortingLayerID is higher that the topSortingLayer...
                if (sortingLayerIDArray[j] >= topSortingLayer)
                {
                    topSortingLayer = sortingLayerIDArray[j];
                    indexOfTopSortingLayer = j;
                }
            }
 
            // Loop through the array of sprite sorting orders...
            for (var k = 0; k < sortingOrderArray.length; k++)
            {
                // If the sorting order of the sprite is higher than topSortingOrder AND the sprite is on the top sorting layer...
                if (sortingOrderArray[k] >= topSortingOrder && sortingLayerIDArray[k] == topSortingLayer)
                {
                    topSortingOrder = sortingOrderArray[k];
                    indexOfTopSortingOrder = k;
                }
                else
                {
                    // Do nothing and continue loop.
                }
            }
 
            // How many sprites with colliders attached are underneath the click?
            Debug.Log("How many sprites have been clicked on: " + hits.length);
               
            // Which is the sorting layer of the frontmost clicked sprite?
            Debug.Log("Frontmost sorting layer ID: "+ topSortingLayer);
               
            // Which is the order in that sorting layer of the frontmost clicked sprite?
            Debug.Log("Frontmost order in layer: "+ topSortingOrder);
 
            // The indexOfTopSortingOrder will also be the index of the frontmost raycast hit in the array "hits".
            return hits[indexOfTopSortingOrder];
        }
        else // If the hits array has a length of 0 then nothing has been clicked...
        {
            Debug.Log("Nothing clicked.");
            return;
        }
    }